import { CommodityListType, GetTeaListParam, GetTypeParam } from "@Tea/model"
import express, { Request, Response } from "express"
import { TypeManageCxt } from "../../context/TypeManageCxt"
import { checkError } from "../../utils/token"
import { CommondityManageCtx } from "../../context/CommondityManageCtx"

const homeRouter = express.Router()

homeRouter.get('/typeList', async (req: Request, res: Response) => {

  try {
    const data = await TypeManageCxt.getInstance().getTypeList({
      typeCode: "",
      typeName: "",
    })

    setTimeout(() => {
      res.send({
        code: 1,
        msg: '查询成功',
        data
      })
    }, Math.random() * 1000)
  } catch (ex) {
    res.status(500).send({
      code: 0,
      msg: checkError(ex)
    })
  }
})

homeRouter.post('/commodityList', async (req: Request, res: Response) => {
  try {
    const params = req.body as GetTeaListParam

    console.log(params);

    const data = await CommondityManageCtx.getInstance().getPageList({
      enable: 1,
      teaName: "",
      teaType: params.teaType,
      currentPage: 1,
      pageSize: params.pageSize
    })

    setTimeout(() => {
      res.send({
        code: 1,
        msg: '查询成功',
        ...data
      })
    }, Math.random() * 1000)
  } catch (ex) {
    res.status(500).send({
      code: 0,
      msg: checkError(ex)
    })
  }
})

homeRouter.get('/teaItemList', async (req: Request, res: Response) => {
  try {
    const teaId = req.query.teaId as any as CommodityListType['teaId']
    const data = await CommondityManageCtx.getInstance().getTeaItems(teaId)

    setTimeout(() => {
      res.send({
        code: 1,
        msg: '查询成功',
        data
      })
    }, Math.random() * 1000)
  } catch (ex) {
    res.status(500).send({
      code: 0,
      msg: checkError(ex)
    })
  }
})

export default homeRouter